package com.mdlib.helper.module.web;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.base.BaseTitleFragment;

import butterknife.Bind;

/**
 * Created by librabin on 16/10/28.
 */

public class WebFragment extends BaseTitleFragment {

    public static final String EXTRA_TITLE = "extra_title";
    public static final String EXTRA_URL = "extra_url";

    @Bind(R.id.webView)
    WebView mWebView;

    @Bind(R.id.progress_bar)
    ProgressBar mProgressBar;

    private String mTitle;
    private String mWebUrl;

    public static WebFragment newInstance(String title, String url) {
        WebFragment fragment = new WebFragment();
        Bundle args = new Bundle();
        args.putString(EXTRA_TITLE, title);
        args.putString(EXTRA_URL, url);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        if (getArguments() != null) {
            mTitle = getArguments().getString(EXTRA_TITLE);
            mWebUrl = getArguments().getString(EXTRA_URL);
        }
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_webview;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        if (!EmptyUtils.isEmpty(mTitle)) {
            setTitleText(mTitle)
                    .setTitleColor(R.color.generic_color_white)
                    .setTitleBgColor(R.color.generic_view_bg)
                    .setImgLeftListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mWebView != null && mWebView.canGoBack()) {
                                mWebView.goBack();
                            } else {
                                removeFragment();
                            }
                        }
                    });
        }

        initWebView();

        mWebView.loadUrl(mWebUrl);
    }


    private void initWebView() {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true); // 启用支持javascript
        webSettings.setAllowFileAccess(true); // 可以访问文件
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
        String ua = webSettings.getUserAgentString();
        webSettings.setUserAgentString(ua + "(MDAPP|PAOTUI)");
        // 先加载网页再加载图片
        if (Build.VERSION.SDK_INT >= 19) {
            mWebView.getSettings().setLoadsImagesAutomatically(true);
        } else {
            mWebView.getSettings().setLoadsImagesAutomatically(false);
        }

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                mProgressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mProgressBar.setVisibility(View.GONE);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress > 90) {
                    mProgressBar.setVisibility(View.GONE);
                }
            }
        });


    }


}
